/**
 * Created by Administrator on 2017/3/22.
 */
const crypto = require('crypto');
/**
 * 非对称加密
 * @type {{verify: module.exports.verify, signer: module.exports.signer}}
 */
module.exports = {
    /**
     * 验签
     * @param sign
     * @param data
     */
    verify:function(sign, data){
        let publicKey = '-----BEGIN PUBLIC KEY-----\r\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwZMHQUgkMxQ3DFnoUsYggn/f1\r\nkscEeHPjsHWCUmzZ/8p8gbRNIm7RPDsYc1h9IyHma9OSb5Ihn6JfBKBIYhgN2XSa\r\noRCvAUoJnyryFbgV+wXx1u73Hlqp0noxhuxOqd+9jSmdbA0JRzbXmhsfucs6KM4t\r\noF4zR/pYvYUkzAUdNwIDAQAB\r\n-----END PUBLIC KEY-----\r\n';
        // let publick = new Buffer(publicKey,'base64');
        let verify = crypto.createVerify('RSA-SHA1');
        let decodedsig = new Buffer(sign, 'base64');
        verify.update(data,'utf8');
        return verify.verify(publicKey, decodedsig, 'hex')
    },
    /**
     * 生成sign
     * @param data
     */
    signer:function(data){
        let privateKey = '-----BEGIN RSA PRIVATE KEY-----\r\nMIICXAIBAAKBgQCwZMHQUgkMxQ3DFnoUsYggn/f1kscEeHPjsHWCUmzZ/8p8gbRN\r\nIm7RPDsYc1h9IyHma9OSb5Ihn6JfBKBIYhgN2XSaoRCvAUoJnyryFbgV+wXx1u73\r\nHlqp0noxhuxOqd+9jSmdbA0JRzbXmhsfucs6KM4toF4zR/pYvYUkzAUdNwIDAQAB\r\nAoGAcfxKOkIo0vWrlPu7XA/09ZAp/akhrkPgL6xh4xs8eAsRYC+4wXeUwOT5Ey3G\r\naub/CxV2nNQ4TD4KAPjOUrgA7i+JEtLI+yv0FeopU3gNPnJK3bvtcidCePG519Ku\r\nEygVtm6ulBQ/eSPGJhQYcoe+rhOK9qdAfn3bXwoa8d8hO2ECQQDVJR02Ojqr58tV\r\nDE098picbtaU5Uv+OBXFCrK9JSQl5PlKf6YRT3JqSxwh7jgk5mT+u3AdRPpG+dql\r\n9c1NPZCPAkEA09v+m+k9/AuPjiEHH7/XP1F7SCj+EaK9hOCV1xPKsP8XdaPkgGcd\r\nfJUDeXEQDo6+J//kJkp0Ht1IJpyatrMs2QJAYYu3v76MZWoSo1ya6RPihg0vxTrf\r\nOXwfcmr//BJIZdNRx6d9sARk/4JaYKjuJu0OIYpUuAB+bgcg6r0AdS4MvwJAQxth\r\nN9OWvbqPu0586jKjJLxTl5TcfYuWCnv1Go57WrtCnBHQ9Fc1QNwWoKYOlv3h/9uP\r\nUg2tYIVpMEnUu+BfaQJBAJZAM/v8tdxMlm32lhc4wy88SrR7toEEWBviuDH0ZncK\r\n7kbC8IHyC8j3ErNJytto6i0tUKBNen4Fa1p58CAoYFk=\r\n-----END RSA PRIVATE KEY-----\r\n';
        let sign = crypto.createSign('RSA-SHA1');
        sign.update(data,'utf8');
        let sig =  sign.sign(privateKey,'hex');
        return sig;
    }
};

